//
//  HTPopView.m
//  Study
//
//  Created by meet on 14-3-6.
//  Copyright (c) 2014年 MEETStudio. All rights reserved.
//

#import "HTPopView.h"

//static NSInteger const kHTpopViewItemWidth = (NSInteger)Screen_Width/2;
NSInteger const kHTPopViewPhoneOverFiveItemHeight = 70;
NSInteger const kHTPopViewPhoneBelowFiveItemHeight = 50;


static NSInteger const kHTPopViewItemButtonBaseTag = 1000;
static CGFloat   const kHTPopViewDismissDurantion = 0.5;
static CGFloat   const kHTPopViewApearDuration = 1.0;
#define kHTpopViewItemWidth       Screen_Width/2

@interface HTPopView ()
@property (nonatomic) NSInteger itemHeight;
//+ (id)popView;
- (void)itemButtonPressed:(id)sender;
@end

@implementation HTPopView


#pragma mark - getter
- (NSInteger)itemHeight
{
    if(IS_PHONE_5_OR_LATER){
        
        _itemHeight = kHTPopViewPhoneOverFiveItemHeight;
    }else{
        
        _itemHeight = kHTPopViewPhoneBelowFiveItemHeight;
    }
    return _itemHeight;
}

#pragma mark - 单例
+ (id)popView
{
    static HTPopView *popView;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        popView = [[HTPopView alloc] init];
    });
    return popView;
}

#pragma mark - 创建
+ (id)popViewWithStringArray:(NSArray *)array
{
    HTPopView *popView = [HTPopView popView];
    popView.frame = CGRectMake(0, 0, kHTpopViewItemWidth, popView.itemHeight*array.count);
    popView.backgroundColor = COLOR_POPITEM;
    
    
    NSArray *oldItemArray = popView.subviews;
    for (int i = 0; i != oldItemArray.count; i++) {
        
        UIButton *oldItemButton = (UIButton *)[popView viewWithTag:i + kHTPopViewItemButtonBaseTag];
        [oldItemButton removeFromSuperview];
    }
    
    for (int i = 0; i != array.count; i++) {
        
        UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
        itemButton.frame = CGRectMake(0, i * popView.itemHeight, kHTpopViewItemWidth, popView.itemHeight);
        itemButton.layer.borderColor = [UIColor grayColor].CGColor;
        itemButton.layer.borderWidth = 1.0;
        [itemButton setTitle:array[i] forState:UIControlStateNormal];
        [itemButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        itemButton.tag = i + kHTPopViewItemButtonBaseTag;
        [itemButton addTarget:popView action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [popView addSubview:itemButton];
    }
    
    return popView;
}


+ (id)popViewWithStringArray:(NSArray *)array _origin:(CGPoint)origin
{
    HTPopView *popView = [self popViewWithStringArray:array];
    
    CGRect frame = popView.frame;
    frame.origin = CGPointMake(origin.x, origin.y-frame.size.height);
    popView.frame = frame;
    popView.alpha = 0;
    [UIView animateWithDuration:1.0 animations:^{
        popView.frame = CGRectMake(origin.x, origin.y, frame.size.width, frame.size.height);
        popView.alpha = 1;
    }];
        
    return popView;
}
#pragma mark - 回调
- (void)itemButtonPressed:(id)sender
{
    UIButton *itemButton = (UIButton *)sender;
    if(_delegate && [_delegate respondsToSelector:@selector(popView:_itemHasClicked:)]){
        
        [_delegate popView:[HTPopView popView] _itemHasClicked:(itemButton.tag - kHTPopViewItemButtonBaseTag)];
    }
    
    if(_delegate && [_delegate respondsToSelector:@selector(popView:_titleForTheClickedItem:)]){
        
        [_delegate popView:[HTPopView popView] _titleForTheClickedItem:itemButton.titleLabel.text];
    }
    
    [[HTPopView popView] dismiss];
}

#pragma mark - 消失
+ (id)dismiss
{
    HTPopView *popView = [HTPopView popView];
    [UIView transitionWithView:popView duration:kHTPopViewDismissDurantion options:UIViewAnimationOptionTransitionCurlUp animations:^{
        popView.alpha = 0;
    } completion:^(BOOL finished) {
        [popView removeFromSuperview];
    }];
    
    return popView;
}

- (void)dismiss
{
    [HTPopView dismiss];
}



@end
